﻿#include "BrowserTableHandlerSpectraAbstract.h"

#include "OperatorsInterface/OperAbstractSpectra.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"

#include <QAction>
#include <QMenu>

namespace GUI
{
    BrowserTableHandlerSpectraAbstract::BrowserTableHandlerSpectraAbstract(WidgetBrowserTable * table):
        BrowserTableAbstractHandler(table)
    {
        if (_physics) {
            _spectraManager = _physics->getFITKAcousticsSpectraManager();
        }
    }

    void GUI::BrowserTableHandlerSpectraAbstract::contextMenu(QMenu * popMenu)
    {
        if (popMenu == nullptr || _tableWidget == nullptr) return;
        QList<QTableWidgetItem*> selectItems = _tableWidget->selectedItems();
        //添加按钮
        QAction* act = this->AddMenuAction(popMenu, tr("Edit"));
        connect(act, SIGNAL(triggered()), this, SLOT(editSpectraSlot()));
        //选择一个的时候，不可以批量可以设置属性
        act = this->AddMenuAction(popMenu, tr("Set Attribute"));
        act->setEnabled(selectItems.size() > 1);
        act = this->AddMenuAction(popMenu, tr("Duplicate"));
        act = this->AddMenuAction(popMenu, tr("Create"));
        connect(act, SIGNAL(triggered()), this, SLOT(createSpectraSlot()));
        act = this->AddMenuAction(popMenu, tr("Copy as Text"));
        connect(act, SIGNAL(triggered()), this, SLOT(copyAsTextSlot()));
        popMenu->addSeparator();
        act = this->AddMenuAction(popMenu, tr("Delete"));
        connect(act, SIGNAL(triggered()), this, SLOT(removeSpectraSlot()));
    }

    void BrowserTableHandlerSpectraAbstract::headerContextMenu(QMenu * popMenu, int coluIndex)
    {
        Q_UNUSED(popMenu);
        Q_UNUSED(coluIndex);
    }

    void BrowserTableHandlerSpectraAbstract::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Bandwidth") << tr("Start (Hz)") << tr("End (Hz)") << tr("Notes");

        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerSpectraAbstract::itemDoubleClicked(int row, int colum)
    {
        EventOper::OperAbstractSpectra* oper = getSpectraOper();
        if (colum > 4 || _tableWidget == nullptr || oper == nullptr)return;
        QTableWidgetItem* selectItem = _tableWidget->item(row, colum);
        if (selectItem == nullptr)return;
        const int id = selectItem->type();
        if (id < 1) return;

        if (colum <= 3) {
            oper->editSpectra(id);
        }
        else {
            oper->editNotes(id);
        }
    }

    QString BrowserTableHandlerSpectraAbstract::getBandWidth(Acoustics::FITKAcousticsAbstractSpectra * spectraData)
    {
        QString bandwidth = tr("");
        if (spectraData == nullptr)return bandwidth;
        switch (spectraData->getDomainType()) {
        case Acoustics::FITKGlobalFrequencyDomain::DTStandardOctave:bandwidth = tr("Octave"); break;
        case Acoustics::FITKGlobalFrequencyDomain::DTStandard1Of3rdOctave:bandwidth = tr("1/3rd Octave"); break;
        case Acoustics::FITKGlobalFrequencyDomain::DTExact1OfnthOctave:
            bandwidth = tr("Exact 1/%1 Octave").arg(spectraData->getOctaveNum()); break;
        case Acoustics::FITKGlobalFrequencyDomain::DTConstantBandWidth:
            bandwidth = tr("Constant - %1 Hz").arg(spectraData->getBandWidth()); break;
        }
        return bandwidth;
    }

    void BrowserTableHandlerSpectraAbstract::editSpectraSlot()
    {
        if (_tableWidget == nullptr) return;
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractSpectra* oper = this->getSpectraOper();
        if (oper == nullptr) return;
        const int matID = selectItem->type();
        if (matID < 1) return;
        //操作器调用
        oper->editSpectra(matID);
    }

    void BrowserTableHandlerSpectraAbstract::createSpectraSlot()
    {
        if (_tableWidget == nullptr) return;
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractSpectra* oper = this->getSpectraOper();
        if (oper == nullptr) return;
        //操作器调用
        oper->createSpectra();
    }

    void BrowserTableHandlerSpectraAbstract::duplicateSpectraSlot()
    {

    }

    void BrowserTableHandlerSpectraAbstract::removeSpectraSlot()
    {
        if (_tableWidget == nullptr) return;
        if (_tableWidget->rowCount() <= 1) {
            AppFrame::FITKMessageError("Please keep at least one");
            return;
        }
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractSpectra* oper = this->getSpectraOper();
        if (oper == nullptr) return;
        const int matID = selectItem->type();
        if (matID < 1) return;
        //操作器调用
        oper->removeSpectra(matID);
        updateData();
    }

    void BrowserTableHandlerSpectraAbstract::setNotesSlot()
    {

    }
}